package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.DefaultAddressGet;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/8/2.
 */
public class DispatchingTypeActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.dispatching_type_express_ll)
    private LinearLayout dispatching_type_express_ll;
    @BoundView(R.id.dispatching_type_express_check_sel_iv)
    private ImageView dispatching_type_express_check_sel_iv;
    @BoundView(R.id.dispatching_type_express_check_no_sel_iv)
    private ImageView dispatching_type_express_check_no_sel_iv;
    @BoundView(R.id.dispatching_type_since_ll)
    private LinearLayout dispatching_type_since_ll;
    @BoundView(R.id.dispatching_type_since_check_sel_iv)
    private ImageView dispatching_type_since_check_sel_iv;
    @BoundView(R.id.dispatching_type_since_check_no_sel_iv)
    private ImageView dispatching_type_since_check_no_sel_iv;
    @BoundView(R.id.dispatching_type_scence_ll)
    private LinearLayout dispatching_type_scence_ll;
    @BoundView(R.id.dispatching_type_scence_check_sel_iv)
    private ImageView dispatching_type_scence_check_sel_iv;
    @BoundView(R.id.dispatching_type_scence_check_no_sel_iv)
    private ImageView dispatching_type_scence_check_no_sel_iv;

    @BoundView(R.id.dispatching_type_default_rl)
    private RelativeLayout dispatching_type_default_rl;
    @BoundView(R.id.dispatching_type_edit_since_ll)
    private LinearLayout dispatching_type_edit_since_ll;

    @BoundView(R.id.dispatching_type_consignee_tv)
    private TextView dispatching_type_consignee_tv;
    @BoundView(R.id.dispatching_type_phone_tv)
    private TextView dispatching_type_phone_tv;
    @BoundView(R.id.dispatching_type_ticket_address_tv)
    private TextView dispatching_type_ticket_address_tv;
    @BoundView(R.id.dispatching_type_submit_tv)
    private TextView dispatching_type_submit_tv;

    //配送地址
    @BoundView(R.id.dispatching_type_name_tv)
    private TextView dispatching_type_name_tv;
    @BoundView(R.id.dispatching_type_address_tv)
    private TextView dispatching_type_address_tv;
    @BoundView(R.id.dispatching_type_tel_tv)
    private TextView dispatching_type_tel_tv;

    @BoundView(R.id.dispatching_bottom_show_tv)
    private TextView dispatching_bottom_show_tv;

    private int dispatchingType = 0;
    private boolean isDefalut = false;
    private String name, phone, address;

    private DefaultAddressGet defaultAddressGet = new DefaultAddressGet(new AsyCallBack<DefaultAddressGet.DefaultAddressEntity>() {

        @Override
        public void onSuccess(String toast, int type, DefaultAddressGet.DefaultAddressEntity defaultAddressEntity) throws Exception {

            if (!TextUtils.isEmpty(defaultAddressEntity.name)) {

                isDefalut = true;

                dispatching_type_name_tv.setText(defaultAddressEntity.name);
                dispatching_type_address_tv.setText(defaultAddressEntity.city + getString(R.string.space) + defaultAddressEntity.address);
                dispatching_type_tel_tv.setText(defaultAddressEntity.phone);

            } else {

                isDefalut = false;

            }

            dispatching_type_ticket_address_tv.setText(defaultAddressEntity.since);

            switch (dispatchingType) {

                case 0:

                    if (!TextUtils.isEmpty(defaultAddressEntity.name)) {

                        isDefalut = true;

                        dispatching_type_name_tv.setText(defaultAddressEntity.name);
                        dispatching_type_address_tv.setText(defaultAddressEntity.city + getString(R.string.space) + defaultAddressEntity.address);
                        dispatching_type_tel_tv.setText(defaultAddressEntity.phone);

                        dispatching_type_default_rl.setVisibility(View.VISIBLE);
                        dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                        dispatchingType = 1;

                    } else {

                        isDefalut = false;

                        dispatching_type_default_rl.setVisibility(View.GONE);
                        dispatching_type_submit_tv.setText(getResources().getString(R.string.receive_address_add_text));

                        dispatchingType = 0;

                    }

                    dispatching_bottom_show_tv.setText("运费说明：订单总价大于200元免运费，道里区、道外区、南岗区、香坊区，运费5元，其他地区全部15元");

                    dispatching_bottom_show_tv.setVisibility(View.VISIBLE);

                    break;

                case 1:

                    isDefalut = true;

                    initCheck();

                    dispatching_type_express_check_no_sel_iv.setVisibility(View.GONE);
                    dispatching_type_express_check_sel_iv.setVisibility(View.VISIBLE);

                    dispatching_type_name_tv.setText(defaultAddressEntity.name);
                    dispatching_type_address_tv.setText(defaultAddressEntity.city + getString(R.string.space) + defaultAddressEntity.address);
                    dispatching_type_tel_tv.setText(defaultAddressEntity.phone);

                    dispatching_type_default_rl.setVisibility(View.VISIBLE);
                    dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                    dispatchingType = 1;

                    dispatching_bottom_show_tv.setText("运费说明：订单总价大于200元免运费，道里区、道外区、南岗区、香坊区，运费5元，其他地区全部15元");

                    dispatching_bottom_show_tv.setVisibility(View.VISIBLE);

                    break;

                case 2:

                    dispatching_type_consignee_tv.setText(name);
                    dispatching_type_phone_tv.setText(phone);

                    initCheck();

                    dispatching_type_since_check_no_sel_iv.setVisibility(View.GONE);
                    dispatching_type_since_check_sel_iv.setVisibility(View.VISIBLE);

                    dispatching_type_default_rl.setVisibility(View.GONE);
                    dispatching_type_edit_since_ll.setVisibility(View.VISIBLE);

                    dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                    dispatchingType = 2;

                    dispatching_bottom_show_tv.setVisibility(View.GONE);

                    break;

                case 3:

                    initCheck();

                    dispatching_type_scence_check_no_sel_iv.setVisibility(View.GONE);
                    dispatching_type_scence_check_sel_iv.setVisibility(View.VISIBLE);

                    dispatching_type_default_rl.setVisibility(View.GONE);
                    dispatching_type_edit_since_ll.setVisibility(View.GONE);

                    dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                    dispatchingType = 3;

                    dispatching_bottom_show_tv.setText("取票地址：" + getIntent().getExtras().getString("place_name") + "（" + getIntent().getExtras().getString("place_address") + "）。");

                    dispatching_bottom_show_tv.setVisibility(View.VISIBLE);

                    break;

            }

            dispatching_type_submit_tv.setVisibility(View.VISIBLE);

//            dispatching_type_express_ll.performClick();

            if (type != 2) {

                switch (type) {

                    case 5:

                        closeForPassResult(5, false);

                        break;

                    case 1:

                        closeForPassResult(1, false);

                        break;

                }

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            Toast.makeText(DispatchingTypeActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dispatching_type);
      //  Toast.makeText(context, GoodsInfoActivity.delivery_way.length() + "", Toast.LENGTH_SHORT).show();
        dispatchingType = getIntent().getExtras().getInt("dispatchingType", 0);

        name = getIntent().getExtras().getString("name", "");
        phone = getIntent().getExtras().getString("phone", "");
//        address = getIntent().getExtras().getString("address", "");

       /* if (GoodsInfoActivity.delivery_way.length() > 4) {
            //快递
            dispatching_type_express_ll.setVisibility(View.VISIBLE);
            //上门
            dispatching_type_since_ll.setVisibility(View.VISIBLE);
//现场
            dispatching_type_scence_ll.setVisibility(View.VISIBLE);
        }else if (GoodsInfoActivity.delivery_way.length()<4 && GoodsInfoActivity.delivery_way.length() >2){
            dispatching_type_express_ll.setVisibility(View.VISIBLE);
            //上门
            dispatching_type_since_ll.setVisibility(View.VISIBLE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.GONE);
        }else if (GoodsInfoActivity.delivery_way.length()<=2 && GoodsInfoActivity.delivery_way.length() >=1)
*/

        if (GoodsInfoActivity.delivery_way.equals("1,2,3")) {
            //快递
            dispatching_type_express_ll.setVisibility(View.VISIBLE);
            //上门
            dispatching_type_since_ll.setVisibility(View.VISIBLE);
//现场
            dispatching_type_scence_ll.setVisibility(View.VISIBLE);
        } else if (GoodsInfoActivity.delivery_way.equals("1,2")) {
            //快递
            dispatching_type_express_ll.setVisibility(View.VISIBLE);
            //上门
            dispatching_type_since_ll.setVisibility(View.VISIBLE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.GONE);
        } else if (GoodsInfoActivity.delivery_way.equals("1")) {
            dispatching_type_express_ll.setVisibility(View.VISIBLE);
            dispatching_type_since_ll.setVisibility(View.GONE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.GONE);
        }else if (GoodsInfoActivity.delivery_way.equals("2")){
            //快递
            dispatching_type_express_ll.setVisibility(View.GONE);
            //上门
            dispatching_type_since_ll.setVisibility(View.VISIBLE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.GONE);
        }else if (GoodsInfoActivity.delivery_way.equals("3")){
            //快递
            dispatching_type_express_ll.setVisibility(View.GONE);
            //上门
            dispatching_type_since_ll.setVisibility(View.GONE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.VISIBLE);
        }else if (GoodsInfoActivity.delivery_way.equals("1,3")){
            //快递
            dispatching_type_express_ll.setVisibility(View.VISIBLE);
            //上门
            dispatching_type_since_ll.setVisibility(View.GONE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.VISIBLE);
        }else if (GoodsInfoActivity.delivery_way.equals("2,3")){
            //快递
            dispatching_type_express_ll.setVisibility(View.GONE);
            //上门
            dispatching_type_since_ll.setVisibility(View.VISIBLE);
            //现场
            dispatching_type_scence_ll.setVisibility(View.VISIBLE);
        }


       /* if (getIntent().getExtras().getBoolean("isShowPost", true)) {

            dispatching_type_express_ll.setVisibility(View.VISIBLE);

        } else {

            dispatching_type_express_ll.setVisibility(View.GONE);

        }

*/
        setBackTrue();

        setTitleName(getResources().getString(R.string.dispatching_type_title_text));

        dispatching_type_express_ll.setOnClickListener(this);
        dispatching_type_since_ll.setOnClickListener(this);
        dispatching_type_scence_ll.setOnClickListener(this);

        dispatching_type_default_rl.setOnClickListener(this);
        dispatching_type_submit_tv.setOnClickListener(this);

        setAppCallBack(new RefreshDefaultAddress());


//        if (dispatchingType == 1 && !TextUtils.isEmpty(getIntent().getExtras().getString("name",""))) {
//
//            dispatching_type_name_tv.setText(getIntent().getExtras().getString("name", ""));
//            dispatching_type_tel_tv.setText(getIntent().getExtras().getString("phone", ""));
//            dispatching_type_address_tv.setText(getIntent().getExtras().getString("address", ""));
//
//        }
//
//
//        if (dispatchingType == 2) {
//
//            dispatching_type_consignee_tv.setText(getIntent().getExtras().getString("name",""));
//            dispatching_type_phone_tv.setText(getIntent().getExtras().getString("phone",""));
//            dispatching_type_ticket_address_tv.setText(getIntent().getExtras().getString("address", ""));
//
//        }

        defaultAddressGet.user_id = BaseApplication.BasePreferences.readUserId();
        defaultAddressGet.execute(this, 2);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (data != null) {

            Bundle bundle = data.getExtras();

            dispatching_type_default_rl.setVisibility(View.VISIBLE);
            dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

            dispatchingType = 1;

            isDefalut = true;

            dispatching_type_name_tv.setText(bundle.getString("name"));
            dispatching_type_address_tv.setText(bundle.getString("address"));
            dispatching_type_tel_tv.setText(bundle.getString("phone"));

        }


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.dispatching_type_express_ll:

                initCheck();

                dispatching_type_express_check_no_sel_iv.setVisibility(View.GONE);
                dispatching_type_express_check_sel_iv.setVisibility(View.VISIBLE);

                dispatching_type_default_rl.setVisibility(View.VISIBLE);
                dispatching_type_edit_since_ll.setVisibility(View.GONE);

                if (isDefalut) {

                    dispatching_type_default_rl.setVisibility(View.VISIBLE);
                    dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                    dispatchingType = 1;

                } else {

                    dispatching_type_default_rl.setVisibility(View.GONE);
                    dispatching_type_submit_tv.setText(getResources().getString(R.string.receive_address_add_text));

                    dispatchingType = 0;

                }

                dispatching_bottom_show_tv.setText("运费说明：订单总价大于200元免运费，道里区、道外区、南岗区、香坊区，运费5元，其他地区全部15元");

                dispatching_bottom_show_tv.setVisibility(View.VISIBLE);

                break;

            case R.id.dispatching_type_since_ll:

                initCheck();

                dispatching_type_since_check_no_sel_iv.setVisibility(View.GONE);
                dispatching_type_since_check_sel_iv.setVisibility(View.VISIBLE);

                dispatching_type_default_rl.setVisibility(View.GONE);
                dispatching_type_edit_since_ll.setVisibility(View.VISIBLE);


                dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                dispatchingType = 2;

                dispatching_bottom_show_tv.setVisibility(View.GONE);

                break;

            case R.id.dispatching_type_scence_ll:


                initCheck();

                dispatching_type_scence_check_no_sel_iv.setVisibility(View.GONE);
                dispatching_type_scence_check_sel_iv.setVisibility(View.VISIBLE);

                dispatching_type_default_rl.setVisibility(View.GONE);
                dispatching_type_edit_since_ll.setVisibility(View.GONE);

                dispatching_type_submit_tv.setText(getResources().getString(R.string.compelete_text));

                dispatchingType = 3;

                dispatching_bottom_show_tv.setText("取票地址：" + getIntent().getExtras().getString("place_name") + "（" + getIntent().getExtras().getString("place_address") + "）。");

                dispatching_bottom_show_tv.setVisibility(View.VISIBLE);

                break;

            case R.id.dispatching_type_default_rl:

                startActivityForResult(new Intent(DispatchingTypeActivity.this, MyReceiveAddressActivity.class).putExtra("isChoose", true), 1);

                break;

            case R.id.dispatching_type_submit_tv:

                if (dispatchingType != 0) {

                    closeForPassResult(dispatchingType, true);

                } else {

//                    Toast.makeText(context, getResources().getString(R.string.dispatching_type_please_choose_text), Toast.LENGTH_SHORT);

                    startActivityForResult(new Intent(DispatchingTypeActivity.this, MyReceiveAddressAddActivity.class).putExtra("isChoose", true), 1);

                    return;
                }

                break;

        }

    }

    private void closeForPassResult(int i, boolean isClose) {


//        UtilLog.e("==========", "选择配送方式里  走一走" + i);

        //数据是使用Intent返回
        Intent intent = new Intent();

        //把返回数据存入Intent
        Bundle bundle = new Bundle();

        switch (i) {
            case 1:

                //TODO  这里写 快递方式的返回值  姓名 电话 邮寄地址

                bundle.putString("dispatching_type_name_tv", dispatching_type_name_tv.getText().toString());
                bundle.putString("dispatching_type_address_tv", dispatching_type_address_tv.getText().toString());
                bundle.putString("dispatching_type_tel_tv", dispatching_type_tel_tv.getText().toString());


                break;

            case 2:


                if (TextUtils.isEmpty(dispatching_type_consignee_tv.getText().toString().trim())) {

                    UtilToast.show(context, getString(R.string.warning_please_fill_goods_people_text));
                    return;

                }

                if (TextUtils.isEmpty(dispatching_type_phone_tv.getText().toString())) {

                    UtilToast.show(context, getString(R.string.warning_please_fill_goods_people_phone_text));
                    return;

                }

                if (!UtilMatches.checkMobile(dispatching_type_phone_tv.getText().toString())) {

                    UtilToast.show(context, getString(R.string.warning_please_fill_true_phone_text));
                    return;
                }

                if (TextUtils.isEmpty(dispatching_type_ticket_address_tv.getText().toString().trim())) {

                    UtilToast.show(context, "取票地址不能为空");
                    return;

                }

                bundle.putString("dispatching_type_consignee_tv", dispatching_type_consignee_tv.getText().toString());
                bundle.putString("dispatching_type_phone_tv", dispatching_type_phone_tv.getText().toString());
                bundle.putString("dispatching_type_ticket_address_tv", dispatching_type_ticket_address_tv.getText().toString());

                break;

            case 3:


                break;

            case 5:


                break;
        }


        intent.putExtras(bundle);
        //设置返回数据
        DispatchingTypeActivity.this.setResult(i, intent);


        if (isClose) {

            //关闭Activity
            this.finish();

        }

    }


    public void initCheck() {

        dispatching_type_express_check_sel_iv.setVisibility(View.GONE);
        dispatching_type_since_check_sel_iv.setVisibility(View.GONE);
        dispatching_type_scence_check_sel_iv.setVisibility(View.GONE);

        dispatching_type_express_check_no_sel_iv.setVisibility(View.VISIBLE);
        dispatching_type_since_check_no_sel_iv.setVisibility(View.VISIBLE);
        dispatching_type_scence_check_no_sel_iv.setVisibility(View.VISIBLE);

    }


    public class RefreshDefaultAddress implements AppCallBack {

        public void RefreshAddress(int isRefresh) {

            switch (isRefresh) {

                case 1:

                    dispatchingType = 1;

                    break;

                case 5:

                    dispatchingType = 0;

                    break;

            }

            defaultAddressGet.user_id = BaseApplication.BasePreferences.readUserId();
            defaultAddressGet.execute(DispatchingTypeActivity.this, isRefresh);

        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
